﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace YY.UI
{
    public class ExcelHelper
    {
        public ExcelHelper() { }

        public class ExcelBE
        {
            private int _row = 0;
            private int _col = 0;
            private string _text = string.Empty;
            private string _startCell = string.Empty;
            private string _endCell = string.Empty;
            private string _interiorColor = string.Empty;
            private bool _isMerge = false;
            private int _size = 0;
            private string _fontColor = string.Empty;
            private string _format = string.Empty;

            public ExcelBE(int row, int col, string text,
              string startCell, string endCell, string interiorColor,
              bool isMerge, int size, string fontColor, string format)
            {
                _row = row;
                _col = col;
                _text = text;
                _startCell = startCell;
                _endCell = endCell;
                _interiorColor = interiorColor;
                _isMerge = isMerge;
                _size = size;
                _fontColor = fontColor;
                _format = format;
            }

            public ExcelBE()
            { }

            public int Row
            {
                get { return _row; }
                set { _row = value; }
            }

            public int Col
            {
                get { return _col; }
                set { _col = value; }
            }

            public string Text
            {
                get { return _text; }
                set { _text = value; }
            }

            public string StartCell
            {
                get { return _startCell; }
                set { _startCell = value; }
            }

            public string EndCell
            {
                get { return _endCell; }
                set { _endCell = value; }
            }

            public string InteriorColor
            {
                get { return _interiorColor; }
                set { _interiorColor = value; }
            }

            public bool IsMerge
            {
                get { return _isMerge; }
                set { _isMerge = value; }
            }

            public int Size
            {
                get { return _size; }
                set { _size = value; }
            }

            public string FontColor
            {
                get { return _fontColor; }
                set { _fontColor = value; }
            }

            public string Formart
            {
                get { return _format; }
                set { _format = value; }
            }

        }

        public class ExcelBase
        {
            private Microsoft.Office.Interop.Excel.Application app = null;
            private Microsoft.Office.Interop.Excel.Workbook workbook = null;
            private Microsoft.Office.Interop.Excel.Worksheet worksheet = null;
            private Microsoft.Office.Interop.Excel.Range workSheet_range = null;

            public ExcelBase()
            {
                createDoc();
            }

            public void createDoc()
            {
                try
                {
                    app = new Microsoft.Office.Interop.Excel.Application();
                    app.Visible = true;
                    workbook = app.Workbooks.Add(1);
                    worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1];
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    workbook.Close(true, Type.Missing, Type.Missing);
                    workbook = null;
                    app.Quit();
                    app = null;
                }
            }

            public void InsertData(ExcelBE be)
            {
                worksheet.Cells[be.Row, be.Col] = be.Text;
                workSheet_range = worksheet.get_Range(be.StartCell, be.EndCell);
                workSheet_range.MergeCells = be.IsMerge;
                workSheet_range.Interior.Color = GetColorValue(be.InteriorColor);
                workSheet_range.Borders.Color = System.Drawing.Color.Black.ToArgb();
                workSheet_range.ColumnWidth = be.Size;
                workSheet_range.Font.Color = string.IsNullOrEmpty(be.FontColor) ?
                 System.Drawing.Color.White.ToArgb() : System.Drawing.Color.Black.ToArgb();
                workSheet_range.NumberFormat = be.Formart;
            }

            private int GetColorValue(string interiorColor)
            {
                switch (interiorColor)
                {
                    case "YELLOW":
                        return System.Drawing.Color.Yellow.ToArgb();
                    case "GRAY":
                        return System.Drawing.Color.Gray.ToArgb();
                    case "GAINSBORO":
                        return System.Drawing.Color.Gainsboro.ToArgb();
                    case "Turquoise":
                        return System.Drawing.Color.Turquoise.ToArgb();
                    case "PeachPuff":
                        return System.Drawing.Color.PeachPuff.ToArgb();

                    default:
                        return System.Drawing.Color.White.ToArgb();
                }
            }
        }
    }
}
